package service.temp.job;

import com.csvreader.CsvWriter;
import org.codehaus.jackson.type.TypeReference;
import utils.JsonUtil;
import service.temp.job.matching.poc.listingapi.dto.SearchItemDetail;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class LoadFromLocal {

    public static void main(String[] args) {
        final Map<String, List<SearchItemDetail>> queryKeywordResultList = loadAllMapFromLocal();
        writeCSV(queryKeywordResultList);
    }

    public static void writeCSV(Map<String, List<SearchItemDetail>> queryKeywordResultList) {
        // 定义一个CSV路径
        String csvFilePath = "/Users/zhfan/Downloads/20220718_merge_result_list_01.csv";
        try {
            // 创建CSV写对象 例如:CsvWriter(文件路径，分隔符，编码格式);
            CsvWriter csvWriter = new CsvWriter(csvFilePath, '#', Charset.forName("UTF-8"));
            // 写表头
            String[] csvHeaders = {"keyword", "response_json"};
            csvWriter.writeRecord(csvHeaders);

            // 写内容
            for (String keyword : queryKeywordResultList.keySet()) {
                String[] csvContent = {keyword, JsonUtil.writeValueAsString(queryKeywordResultList.get(keyword)).replaceAll("#", "@_@")};
                csvWriter.writeRecord(csvContent);
            }

            csvWriter.close();
            System.out.println("--------CSV文件已经写入--------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void writeToLocal(String fileName, List<String> contentList) {
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
            for (String content : contentList) {
                out.write(content + "\n");
            }
            out.close();
            System.out.println("文件创建成功！");
        } catch (Exception e) {
            System.err.println("error meet" + e.toString());
        }
    }


    public static Map<String, List<SearchItemDetail>> loadAllMapFromLocal() {
        Map<String, List<SearchItemDetail>> wholeMap = new HashMap<>();

        File dir = new File("/Users/zhfan/Documents/cache01");
        String[] children = dir.list();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                String fileName = "/Users/zhfan/Documents/cache01/" + children[i];
                Map<String, List<SearchItemDetail>> partMap = loadSearchCacheMapFromLocal(fileName);
                System.out.println("partMap.size : " + partMap.size());
                wholeMap.putAll(partMap);
            }
        }

        return wholeMap;
    }

    public static Map<String, List<SearchItemDetail>> loadSearchCacheMapFromLocal(String fileName) {
        Map<String, List<SearchItemDetail>> searchMapping = new HashMap<>();
        searchMapping = JsonUtil.readValue(new File(fileName), new TypeReference<Map<String, List<SearchItemDetail>>>() {
        });

        return searchMapping;
    }

}
